package com.xs.ahoi.hcxs.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.utils.TimerDownUtil;

/**
 * 定时停止播放
 */
public class BookTimerActivity extends BaseActivity {

    @BindView(R.id.tvCancel)
    public TextView tvCancel;
    @BindView(R.id.tvShowTime)
    public TextView tvShowTime;
    @BindView(R.id.tvTimer00)
    public TextView tvTimer00;
    @BindView(R.id.tvTimer01)
    public TextView tvTimer01;
    @BindView(R.id.tvTimer02)
    public TextView tvTimer02;
    @BindView(R.id.tvTimer03)
    public TextView tvTimer03;
    @BindView(R.id.tvTimer04)
    public TextView tvTimer04;
    @BindView(R.id.tvTimer05)
    public TextView tvTimer05;
    @BindView(R.id.tvTimer06)
    public TextView tvTimer06;
    @BindView(R.id.tvTimer07)
    public TextView tvTimer07;
    @BindView(R.id.tvTimer08)
    public TextView tvTimer08;
    @BindView(R.id.tvTimer09)
    public TextView tvTimer09;
    @BindView(R.id.tvTimer10)
    public TextView tvTimer10;
    @BindView(R.id.tvTimer11)
    public TextView tvTimer11;
    @BindView(R.id.tvTimer12)
    public TextView tvTimer12;
    @BindView(R.id.tvTimer13)
    public TextView tvTimer13;
    @BindView(R.id.tvTimer14)
    public TextView tvTimer14;
    @BindView(R.id.tvTimer15)
    public TextView tvTimer15;
    @BindView(R.id.tvTimer16)
    public TextView tvTimer16;
    @BindView(R.id.tvTimer17)
    public TextView tvTimer17;
    @BindView(R.id.tvTimer18)
    public TextView tvTimer18;
    @BindView(R.id.tvTimer19)
    public TextView tvTimer19;

    // 按钮的选中样式
    private Drawable timerBtnNormal, timerBtnFocus;
    // 倒计时工具
    private TimerDownUtil timerDownUtil;
    // 按钮数组
    private TextView[] btnArrays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_timer);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        btnArrays = new TextView[]{tvTimer00, tvTimer01, tvTimer02, tvTimer03, tvTimer04, tvTimer05,
                tvTimer06, tvTimer07, tvTimer08, tvTimer09, tvTimer10, tvTimer11, tvTimer12,
                tvTimer13, tvTimer14, tvTimer15, tvTimer16, tvTimer17, tvTimer18, tvTimer19};

        timerBtnNormal = ContextCompat.getDrawable(this, R.drawable.shape_timer_btn_normal);
        timerBtnFocus = ContextCompat.getDrawable(this, R.drawable.shape_timer_btn_focus);

        timerDownUtil = TimerDownUtil.getInstance();
        timerDownUtil.setCallBack(new TimerDownUtil.CallBack() {
            @Override
            public void onTick(long milliSecond) {
                showTime(milliSecond / 1000);
            }

            @Override
            public void onFinish() {
                cancelTime();
            }
        });

        // 初始化倒计时默认显示
        long leaveSecond = timerDownUtil.getLeaveSecond();
        if (leaveSecond > -1) {
            showTime(leaveSecond);
            tvCancel.setVisibility(View.VISIBLE);
            // 回显上次点击的按钮下标
            btnArrays[timerDownUtil.getBtnArrayIndex()].setBackground(timerBtnFocus);
        }
    }

    @OnClick({R.id.tvTimer00, R.id.tvTimer01, R.id.tvTimer02, R.id.tvTimer03, R.id.tvTimer04,
            R.id.tvTimer05, R.id.tvTimer06, R.id.tvTimer07, R.id.tvTimer08, R.id.tvTimer09,
            R.id.tvTimer10, R.id.tvTimer11, R.id.tvTimer12, R.id.tvTimer13, R.id.tvTimer14,
            R.id.tvTimer15, R.id.tvTimer16, R.id.tvTimer17, R.id.tvTimer18, R.id.tvTimer19,
            R.id.tvCancel, R.id.arrowLeft})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.arrowLeft:
                super.onBackPressed();
                break;
            case R.id.tvCancel:
                cancelTime();
                break;
            case R.id.tvTimer00:
                countDownTime(5 * 60);
                setFocusBgStyle(0);
                break;
            case R.id.tvTimer01:
                countDownTime(10 * 60);
                setFocusBgStyle(1);
                break;
            case R.id.tvTimer02:
                countDownTime(15 * 60);
                setFocusBgStyle(2);
                break;
            case R.id.tvTimer03:
                countDownTime(20 * 60);
                setFocusBgStyle(3);
                break;
            case R.id.tvTimer04:
                countDownTime(30 * 60);
                setFocusBgStyle(4);
                break;
            case R.id.tvTimer05:
                countDownTime(45 * 60);
                setFocusBgStyle(5);
                break;
            case R.id.tvTimer06:
                countDownTime(60 * 60);
                setFocusBgStyle(6);
                break;
            case R.id.tvTimer07:
                countDownTime(90 * 60);
                setFocusBgStyle(7);
                break;
            case R.id.tvTimer08:
                countDownTime(120 * 60);
                setFocusBgStyle(8);
                break;
            case R.id.tvTimer09:
                countDownTime(150 * 60);
                setFocusBgStyle(9);
                break;
            default:
                break;
        }
    }

    // 设置按钮的按下样式
    private void setFocusBgStyle(int arrayIndex) {
        for (TextView view : btnArrays) {
            view.setBackground(timerBtnNormal);
        }

        if (arrayIndex >= 0 && arrayIndex < btnArrays.length) {
            TextView view = btnArrays[arrayIndex];
            view.setBackground(timerBtnFocus);

            // 记录上次点击的按钮下标
            timerDownUtil.setBtnArrayIndex(arrayIndex);
        }
    }

    // 根据秒数，转换为时分格式倒计时
    private void showTime(long second) {
        String time = "";
        long h = second / 3600;
        long m = (second - h * 3600) / 60;
        long s = second - h * 3600 - m * 60;
        if (h > 0) {
            time += h + ":";
        }
        if (m > 0) {
            time += (m > 9 ? m : "0" + m) + ":";
        } else {
            time += "00:";
        }
        time += s > 9 ? s : "0" + s;
        tvShowTime.setText(time);
    }

    // 倒计时
    private void countDownTime(long second) {
        timerDownUtil.start(second);
        tvCancel.setVisibility(View.VISIBLE);
    }

    // 取消倒计时
    private void cancelTime() {
        timerDownUtil.cancel();
        timerDownUtil.init();
        setFocusBgStyle(-1);
        tvShowTime.setText("00:00");
        tvCancel.setVisibility(View.GONE);
    }
}
